package net.spunofbook.negocio;

import java.util.List;
import net.spunofbook.R;
import net.spunofbook.TelaClienteCadastro;
import net.spunofbook.modelo.ClienteRepositorio;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("ViewHolder")
public class ClienteAdapter extends BaseAdapter {

	private Context context;
	private List<Cliente> clientes;

	public ClienteAdapter(Context context, List<Cliente> clientes) {
		this.context = context;
		this.clientes = clientes;
	}


	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int auxPosition = position;

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.tela_cliente_base_adapter, null);

		TextView nome = (TextView) layout.findViewById(R.id.telaClienteBaseAdapter_nome);
		nome.setText(clientes.get(position).getNome().toString());
		
		TextView telefone = (TextView) layout.findViewById(R.id.telaClienteBaseAdapter_telefone);
		telefone.setText(clientes.get(position).getTelefone());
		
		
		Button botaoDeletar = (Button) layout.findViewById(R.id.telaClienteBaseAdapter_deletar);
		botaoDeletar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ClienteRepositorio queryCliente = new ClienteRepositorio(context);
				queryCliente.deletar(clientes.get(auxPosition));
				
				layout.setVisibility(View.GONE);
			}
		});
		
		
		Button botaoEditar = (Button) layout.findViewById(R.id.telaClienteBaseAdapter_editar);
		botaoEditar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, TelaClienteCadastro.class);
				intent.putExtra("nome", clientes.get(auxPosition).getNome());
				intent.putExtra("telefone", clientes.get(auxPosition).getTelefone());
				intent.putExtra("id", clientes.get(auxPosition).getId());
				intent.putExtra("endereco", clientes.get(auxPosition).getEndereco());
				intent.putExtra("cpf", clientes.get(auxPosition).getCpf());
				context.startActivity(intent);
			}
		});
		/**/
		return layout;
	}

	@Override
	public int getCount() {
		return clientes.size();
	}

	@Override
	public Object getItem(int position) {
		return clientes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return clientes.get(position).getId();
	}

}
